package com.tcgroup.phone.base_core.widget;

import android.content.Context;
import android.os.Build;
import android.util.ArrayMap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.GridLayout;

import androidx.annotation.RequiresApi;

import com.tcgroup.phone.base_core.R;
import com.tcgroup.phone.base_core.widget.model.SmartRadioGroupModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者: wangfei
 * 创建时期: 2020/4/23.
 * 包名: com.tcgroup.phone.base_core.widget
 * 类名: OSAndroidMvvm
 */
public class SmartCheckBoxGridLayout extends GridLayout {

    public ArrayMap<String,String> checkValue = new ArrayMap<>();
    public ArrayList<SmartRadioGroupModel> list;
    public GetCheckBoxIdsListener getCheckBoxIdsListener;

    public SmartCheckBoxGridLayout(Context context) {
        super(context);
    }

    public SmartCheckBoxGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SmartCheckBoxGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SmartCheckBoxGridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public ArrayMap<String, String> getCheckValue() {
        return checkValue;
    }

    public void setCheckValue(ArrayMap<String, String> checkValue) {
        this.checkValue = checkValue;
    }

    public ArrayList<SmartRadioGroupModel> getList() {
        return list;
    }

    public GetCheckBoxIdsListener getGetCheckBoxIdsListener() {
        return getCheckBoxIdsListener;
    }

    public void setGetCheckBoxIdsListener(GetCheckBoxIdsListener mGetCheckBoxIdsListener) {
        this.getCheckBoxIdsListener = mGetCheckBoxIdsListener;
    }

    public void setList(ArrayList<SmartRadioGroupModel> list) {
        this.list = list;
        if (null == list || list.size() <=0){
            return;
        }
        for (SmartRadioGroupModel model : list){
            View view = LayoutInflater.from(getContext()).inflate(R.layout.core_widget_layout_cb,null,false);
            final CheckBox checkBox = view.findViewById(R.id.checkbox);
            checkBox.setText(model.getName());
            checkBox.setTag(model.getValue());
            checkBox.setOnCheckedChangeListener(
                    (compoundButton, b) -> {
                        if (b) {
                            checkValue.put(model.getValue(),model.getValue());
                        }else {
                            checkValue.remove(model.getValue());
                        }
                        if (getCheckBoxIdsListener != null){
                            getCheckBoxIdsListener.getIds(getCheckBoxId());
                        }
                    });
            addView(view);
        }
    }

    public int[] getCheckBoxId(){
        List<String> list = new ArrayList<>(checkValue.values());
        int[] dir = new int[list.size()];
        int i = 0;
        for (String value : list){
            dir[i] = Integer.valueOf(value);
            i++;
        }
        return dir;
    }

    public interface GetCheckBoxIdsListener{
        void getIds(int[] dirs);
    }
}
